package note1.chapters.chapter4;

public class Demo3 {
	public static void main(String[] args) {
		long s3 = System.nanoTime();
		method3();
		long d3 = System.nanoTime();
		System.out.println("method3 :" + (d3 - s3));// method3 :574735
	}

	private static void method3() { // 官方参考答案
		int[] startDigit = new int[4];
		int[] productDigit = new int[4];
		for (int num1 = 10; num1 <= 99; num1++)
			for (int num2 = num1; num2 <= 99; num2++) {
				// Pete Hartley's theoretical result:
				// If x·y is a vampire number then
				// x·y == x+y (mod 9)
				if ((num1 * num2) % 9 != (num1 + num2) % 9)
					continue;
				int product = num1 * num2;
				startDigit[0] = num1 / 10;
				startDigit[1] = num1 % 10;
				startDigit[2] = num2 / 10;
				startDigit[3] = num2 % 10;
				productDigit[0] = product / 1000;
				productDigit[1] = product % 1000 / 100;
				productDigit[2] = product % 100 / 10;
				productDigit[3] = product % 10;
				int count = 0;
				for (int x = 0; x < 4; x++)
					for (int y = 0; y < 4; y++) {
						if (productDigit[x] == startDigit[y]) {
							count++;
							productDigit[x] = -1;
							startDigit[y] = -2;
							if (count == 4)
								System.out.println(num1 + " * " + num2 + " : "
										+ product);
						}
					}

			} /*
			 * Output: 15 * 93 : 1395 21 * 60 : 1260 21 * 87 : 1827 27 * 81 :
			 * 2187 30 * 51 : 1530 35 * 41 : 1435 80 * 86 : 6880
			 */// :~
	}

}
